using log.Nlog.ILogger;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace log.Nlog.Logger
{
    public class NLogLoggerService : ILoggerService
    {
        private static readonly NLog.Logger s_logger = LogManager.GetCurrentClassLogger();

        // 实现ILoggerService接口的所有方法
        public void LogDebug(string message) => s_logger.Debug(message);
        public void LogInfo(string message) => s_logger.Info(message);
        public void LogWarning(string message) => s_logger.Warn(message);
        public void LogError(string message) => s_logger.Error(message);
        public void LogError(Exception ex, string message = null) => s_logger.Error(ex, message);
        
        // 实现Error(Exception)方法
        public void Error(Exception ex)
        {
            Log(LogLevel.Error, LogType.系统异常, ex.Message);
        }

        /// <summary>
        /// 配置Logger
        /// </summary>
        // static NLogLoggerService()
        // {
        //     // 使用新的NLog配置加载API
        //     var config = new LoggingConfiguration();
        //     
        //     // 添加控制台目标
        //     var consoleTarget = new ConsoleTarget();
        //     config.AddTarget(consoleTarget);
        //     
        //     // 简化日志级别设置，直接使用NLog内置的日志级别
        //     config.AddRuleForAllLevels(consoleTarget);
        //     
        //     // 应用配置
        //     LogManager.Configuration = config;
        // // }

        public void Log(LogLevel logLevel, LogType logType, string msg, string data)
        {
            NLog.Logger _nLogger = LogManager.GetLogger(logType.ToString());

            var log = new NLog.LogEventInfo(NLog.LogLevel.FromString(logLevel.ToString()), logType.ToString(), msg);
            log.Properties[LoggerConfig.s_data] = data;
            log.Properties[LoggerConfig.s_logType] = logType.ToString();
            try
            {
                // TODO: ContainerLocator 和 IOperator 依赖未找到定义，需要补充相关引用或实现
                //if (_operator != null && _operator.Property != null)
                //{\r\n                //    log.Properties[LoggerConfig.CreatorName] = _operator?.UserName;\r\n                //    log.Properties[LoggerConfig.CreatorId] = _operator?.Property?.Id;\r\n                //}
            }
            catch
            {

            }

            _nLogger.Log(log);
        }

        public void Log(LogLevel logLevel, LogType logType, string msg)
        {
            Log(logLevel, logType, msg, null);
        }

        public void Trace(LogType logType, string msg)
        {
            Log(LogLevel.Trace, logType, msg);
        }

        public void Trace(LogType logType, string msg, string data)
        {
            Log(LogLevel.Trace, logType, msg, data);
        }

        public void Debug(LogType logType, string msg)
        {
            Log(LogLevel.Debug, logType, msg);
        }

        public void Debug(LogType logType, string msg, string data)
        {
            Log(LogLevel.Debug, logType, msg, data);
        }

        public void Info(LogType logType, string msg)
        {
            Log(LogLevel.Info, logType, msg);
        }

        public void Info(LogType logType, string msg, string data)
        {
            Log(LogLevel.Info, logType, msg, data);
        }

        public void Warn(LogType logType, string msg)
        {
            Log(LogLevel.Warn, logType, msg);
        }

        public void Warn(LogType logType, string msg, string data)
        {
            Log(LogLevel.Warn, logType, msg, data);
        }

        public void Error(LogType logType, string msg)
        {
            Log(LogLevel.Error, logType, msg);
        }

        public void Error(LogType logType, string msg, string data)
        {
            Log(LogLevel.Error, logType, msg, data);
        }

        public void Fatal(LogType logType, string msg)
        {
            Log(LogLevel.Fatal, logType, msg);
        }

        public void Fatal(LogType logType, string msg, string data)
        {
            Log(LogLevel.Fatal, logType, msg, data);
        }

        // 补充实现其他接口方法
        public void LogCritical(string message)
        {
            s_logger.Fatal(message);
        }

        public void LogCritical(Exception ex, string message = null)
        {
            s_logger.Fatal(ex, message ?? ex.Message);
        }

        public void LogCustom(LogLevel level, string source, string message, string data = null)
        {
            var logLevel = NLog.LogLevel.FromString(level.ToString());

            var logger = LogManager.GetLogger(source);

            var logEvent = new LogEventInfo(logLevel, logger.Name, message);
            logEvent.Properties[LoggerConfig.s_data] = data;
            logEvent.Properties[LoggerConfig.s_logType] = source;

            logger.Log(logEvent);
        }
    }
}


